<template>
  <page-header-wrapper>
    <a-card :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="合同编号">
                <a-input v-model="queryParam.contractNo" placeholder="" />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="模板编号">
                <a-input v-model="queryParam.templateKeyword" placeholder="" />
              </a-form-item>
            </a-col>

            <template v-if="advanced">
              <a-col :md="8" :sm="24">
                <a-form-item label="订单编号">
                  <a-input v-model="queryParam.merchantContractNo" placeholder="" />
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="商户">
                  <a-input v-model="queryParam.merchantKeyword" placeholder="" />
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="还款状态">
                  <a-select v-model="queryParam.repaymentStatus" placeholder="请选择">
                    <a-select-option value="1">正常</a-select-option>
                    <a-select-option value="2">逾期</a-select-option>
                    <a-select-option value="3">结清</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="配资状态">
                  <a-select v-model="queryParam.capitalContractStatus" placeholder="请选择">
                    <a-select-option value="0">未打包</a-select-option>
                    <a-select-option value="1">已打包</a-select-option>
                    <a-select-option value="2">已结清</a-select-option>
                    <a-select-option value="4">已冻结</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="服务费状态">
                  <a-select v-model="queryParam.capitalContractStatus" placeholder="请选择">
                    <a-select-option value="0">未支付</a-select-option>
                    <a-select-option value="1">已支付</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="签约开始时间">
                  <a-date-picker
                    v-model="queryParam.startSignCompleteTime"
                    style="width: 100%"
                    placeholder="签约开始时间"
                  />
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="签约结束时间">
                  <a-date-picker
                    v-model="queryParam.endSignCompleteTime"
                    style="width: 100%"
                    placeholder="签约结束时间"
                  />
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="创建开始时间">
                  <a-date-picker v-model="queryParam.startCreateTime" style="width: 100%" placeholder="创建开始时间" />
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="创建结束时间">
                  <a-date-picker v-model="queryParam.endCreateTime" style="width: 100%" placeholder="创建结束时间" />
                </a-form-item>
              </a-col>
            </template>

            <a-col :md="8" :sm="24">
              <span
                class="table-page-search-submitButtons"
                :style="(advanced && { float: 'right', overflow: 'hidden' }) || {}"
              >
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => (this.queryParam = {})">重置</a-button>
                <a @click="toggleAdvanced" style="margin-left: 8px">
                  {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'" />
                </a>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <s-table
        ref="table"
        size="default"
        rowKey="contractNo"
        :columns="columns"
        :data="loadData"
        :alert="false"
        showPagination="auto"
        listName="dataList"
        :scroll="{ x: 'calc(1600px + 100%)' }"
        v-if="btnsPurviewData.query.isShow"
      >
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 }}
        </span>
        <div slot="userInfo" slot-scope="text, record">
          <div>{{ record.userName }}</div>
          <div>{{ record.userMobile }}</div>
        </div>
        <div slot="merchantInfo" slot-scope="text, record">
          <div>{{ record.merchantName }}</div>
          <div>
            <span>{{ record.merchantContactName }}</span
            >&nbsp;&nbsp;<span>{{ record.merchantContactMobile }}</span>
          </div>
        </div>
        <span slot="serviceFeeAmount" slot-scope="text, record">
          <span>{{
            text ? `${text}/${((record.serviceFeeAmount * 100) / record.contractAmount).toFixed(2)}` : '—'
          }}</span>
        </span>
        <span slot="repaymentDay" slot-scope="text">
          <span>{{ text | repaymentDayFilter }}</span>
        </span>
        <span slot="serviceFeePayStatus" slot-scope="text">
          <span>{{ text | serviceFeePayStatusFilter }}</span>
        </span>
        <span slot="repaymentStatus" slot-scope="text">
          <span>{{ text | repaymentStatusFilter }}</span>
        </span>
        <span slot="capitalContractStatus" slot-scope="text">
          <span>{{ text | capitalContractStatusFilter }}</span>
        </span>
      </s-table>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import { BTNS_PURVIEW_MAP } from '@/store/mutation-types'
import storage from 'store'
import { STable, Ellipsis } from '@/components'
import { getCustContractList } from '@/api/merchantContractManage'

const columns = [
  {
    title: '序号',
    scopedSlots: { customRender: 'serial' },
    align: 'center',
    fixed: 'left',
    width: 80
  },
  {
    title: '签约人信息',
    scopedSlots: { customRender: 'userInfo' },
    align: 'center'
  },
  {
    title: '所属商户',
    scopedSlots: { customRender: 'merchantInfo' },
    align: 'center'
  },
  {
    title: '合同编号',
    dataIndex: 'contractNo',
    customRender: (text) => text || '—',
    align: 'center'
  },
  {
    title: '合同金额（元）',
    dataIndex: 'contractAmount',
    customRender: (text) => text || '—',
    align: 'center'
  },
  {
    title: '定金',
    dataIndex: 'depositAmount',
    customRender: (text) => text || '—',
    align: 'center'
  },
  {
    title: '分期选择',
    dataIndex: 'repaymentCycle',
    customRender: (text) => text || '—',
    align: 'center'
  },
  {
    title: '还款日',
    dataIndex: 'repaymentDay',
    scopedSlots: { customRender: 'repaymentDay' },
    align: 'center'
  },
  {
    title: '服务费',
    dataIndex: 'serviceFeeAmount',
    scopedSlots: { customRender: 'serviceFeeAmount' },
    align: 'center'
  },
  {
    title: '服务费支付状态',
    dataIndex: 'serviceFeePayStatus',
    scopedSlots: { customRender: 'serviceFeePayStatus' },
    align: 'center'
  },
  {
    title: '还款状态',
    dataIndex: 'repaymentStatus',
    scopedSlots: { customRender: 'repaymentStatus' },
    align: 'center'
  },
  {
    title: '配资状态',
    dataIndex: 'capitalContractStatus',
    scopedSlots: { customRender: 'capitalContractStatus' },
    align: 'center'
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
    customRender: (text) => text || '—',
    align: 'center'
  }
]

const statusMap = {
  1: {
    status: 'processing',
    text: '启用'
  },
  2: {
    status: 'default',
    text: '冻结'
  }
}

const serviceFeePayStatusMap = {
  0: {
    text: '未支付'
  },
  1: {
    text: '已支付'
  }
}

const repaymentStatusMap = {
  1: {
    text: '正常'
  },
  2: {
    text: '逾期'
  },
  3: {
    text: '结清'
  }
}

const capitalContractStatusMap = {
  0: {
    text: '未打包'
  },
  1: {
    text: '已打包'
  },
  2: {
    text: '已结清'
  },
  3: {
    text: '已冻结'
  }
}

const repaymentDayMap = {
  1: {
    text: '次月今日'
  },
  2: {
    text: '次月5日'
  },
  3: {
    text: '次月15日'
  },
  4: {
    text: '次月25日'
  },
  5: {
    text: '一周后'
  },
  6: {
    text: '本月月底'
  }
}

export default {
  name: 'MerchantContractCustContractList',
  components: {
    STable,
    Ellipsis
  },
  data () {
    this.columns = columns
    return {
      btnsPurviewMap: storage.get(BTNS_PURVIEW_MAP),
      btnsPurviewData: {},
      // create model
      visible: false,
      freezeVisible: false,
      confirmLoading: false,
      mdl: null,
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {},
      // 加载数据方法 必须为 Promise 对象
      loadData: (parameter) => {
        const requestParameters = Object.assign({}, parameter, this.queryParam)
        return getCustContractList(requestParameters)
          .then((res) => {
            return res.result
          })
          .catch((e) => {})
      },
      selectedRowKeys: [],
      selectedRows: []
    }
  },
  filters: {
    statusFilter (type) {
      return statusMap[type].text
    },
    statusTypeFilter (type) {
      return statusMap[type].status
    },
    serviceFeePayStatusFilter (type) {
      return serviceFeePayStatusMap[type].text
    },
    repaymentStatusFilter (type) {
      return repaymentStatusMap[type].text
    },
    capitalContractStatusFilter (type) {
      return capitalContractStatusMap[type].text
    },
    repaymentDayFilter (type) {
      return repaymentDayMap[type].text
    }
  },
  created () {
    this.btnsPurviewData = this.btnsPurviewMap[this.$route.meta.permission[0]]
  },
  computed: {},
  methods: {
    /**
     * 切换searchBar展开/关闭
     */
    toggleAdvanced () {
      this.advanced = !this.advanced
    }
  }
}
</script>
